package client

import (
	"fmt"
	"net"
	"time"
)

type Client struct {
	Ip        string
	Port      int
	LocalPort int
}

func NewClient(ip string, port int, localPort int) *Client {
	return &Client{
		Ip:        ip,
		Port:      port,
		LocalPort: localPort,
	}
}

func (c *Client) Start() {
	netAddr := &net.TCPAddr{Port: c.LocalPort}
	dialer := net.Dialer{LocalAddr: netAddr}
	conn, err := dialer.Dial("tcp", fmt.Sprintf("%s:%d", c.Ip, c.Port))
	if err != nil {
		fmt.Println("connection server err")
	}
	defer conn.Close()
	//监听消息
	go c.listenMessage(conn)
	c.SystemConsole(conn)

}

func printLevel1() {
	fmt.Println("|--------------选择菜单---------------|")
	fmt.Println("|------------1.查询我的群聊-----------|")
	fmt.Println("|------------2.创建我的群聊-----------|")
	fmt.Println("|------------3.删除我的群聊-----------|")
	fmt.Println("|------------4.群聊中发消息-----------|")
	fmt.Println("|------------5.查询在线人员-----------|")
}

func (c *Client) SystemConsole(conn net.Conn) {
	//输入命令
	var console string
	for {
		//打印菜单
		printLevel1()
		fmt.Print("|------------请选择菜单:")
		fmt.Scanln(&console)
		switch console {
		case "1":
			c.myHouseClient(conn)
		case "2":
			c.createHouseClient(conn)
		case "3":
			c.delHouseClient(conn)
		case "4":
			c.msgHouseClient(conn)
		case "5":
			c.userListClient(conn)
		}
	}
}

func (c *Client) userListClient(conn net.Conn) {
	var next string
	for {
		connWrite(fmt.Sprintf("user_list"), conn)
		fmt.Print("|------------请选择操作:1.再次查询 2.返回上级菜单")
		fmt.Scanln(&next)
		switch next {
		case "1":
			continue
		case "2":
			return
		}
	}
}

func (c *Client) msgHouseClient(conn net.Conn) {
	var houseName string
	var message string
	var next string
	for {
		connWrite(fmt.Sprintf("my_houses"), conn)
		fmt.Print("|------------请输入房间名称:")
		fmt.Scanln(&houseName)
		fmt.Print("|------------请输入消息内容:")
		fmt.Scanln(&message)
		connWrite(fmt.Sprintf("send_msg:=%s:=%s", houseName, message), conn)
		fmt.Print("|------------请选择操作:1.再发一条 2.返回上级菜单")
		fmt.Scanln(&next)
		switch next {
		case "1":
			continue
		case "2":
			return
		}
	}
}

func (c *Client) delHouseClient(conn net.Conn) {
	var houseName string
	var next string
	for {
		fmt.Print("|------------请输入房间名称:")
		fmt.Scanln(&houseName)
		connWrite(fmt.Sprintf("del_house:=%s", houseName), conn)
		fmt.Print("|------------请选择操作:1.继续删除 2.返回上级菜单")
		fmt.Scanln(&next)
		switch next {
		case "1":
			continue
		case "2":
			return
		}
	}
}

func (c *Client) myHouseClient(conn net.Conn) {
	var next string
	for {
		connWrite(fmt.Sprintf("my_houses"), conn)
		fmt.Print("|------------请选择操作:1.继续查询 2.返回上级菜单")
		fmt.Scanln(&next)
		switch next {
		case "1":
			continue
		case "2":
			return
		}
	}
}

func (c *Client) createHouseClient(conn net.Conn) {
	var users string
	var houseName string
	var next string
	for {
		fmt.Print("|------------请输入房间名称:")
		fmt.Scanln(&houseName)
		fmt.Print("|------------请输入用户名(多个逗号隔开):")
		fmt.Scanln(&users)
		connWrite(fmt.Sprintf("create_house:=%s:=%s", users, houseName), conn)
		fmt.Print("|------------请选择操作:1.继续创建 2.返回上级菜单")
		fmt.Scanln(&next)
		switch next {
		case "1":
			continue
		case "2":
			return
		}
	}
}

func (c *Client) listenMessage(conn net.Conn) {
	bytes := make([]byte, 4096)
	for {
		read, err := conn.Read(bytes)
		if read == 0 {
			continue
		}
		if err != nil {
			fmt.Println("read msg error")
			continue
		}
		msg := string(bytes[:read])
		fmt.Println("\n|---------收到消息:" + msg)
	}
}

func connWrite(console string, conn net.Conn) {
	conn.Write([]byte(console))
	time.Sleep(time.Second)
}
